package com.itehima.springbootcondition.condition;


import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;
import redis.clients.jedis.Jedis;

import java.util.Map;

public class ClassCondition implements Condition {
    /**
     *
     * @param context 用于获取环境,ioc容器,classloader对象
     * @param metadata 注解元对象,用于获取注解定义的属性值
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
//        boolean flag=true;
//        try {
//            Class.forName("redis.clients.jedis.Jedis");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//            flag=false;
//        }
//        return flag;

        boolean flag=true;
        try {
            Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
            String[] value = (String[])map.get("value");
            for (String className : value) {
                Class<?> aClass = Class.forName(className);
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }
}
